package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/admin/user")
@RestController
public class AdminUserController{

    @Autowired
    AdminUserService adminUserService;

    @PostMapping("/update")
    @RequiresPermissions("admin:user:update")
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"},button = "更新")
    public Object update(HttpServletRequest req) throws IOException {
        String s = req.getReader().readLine();
        String nickname = JacksonUtil.parseString(s, "nickname");
        String status = JacksonUtil.parseString(s,"status");
        String gender = JacksonUtil.parseString(s, "gender");
        String mobile = JacksonUtil.parseString(s, "mobile");
        String password = JacksonUtil.parseString(s, "password");
        String id = JacksonUtil.parseString(s, "id");
        List<String> updateList = new ArrayList<>();
        updateList.add("nickname"+":"+nickname);
        updateList.add("status"+":"+status);
        updateList.add("gender"+":"+gender);
        updateList.add("mobile"+":"+mobile);
        updateList.add("password"+":"+password);
        adminUserService.update(nickname,status,gender,password,mobile,id);


        Map<String,Integer> map = new HashMap<>();
        map.put("data",1);
        return JacksonUtil.writeValueAsString(ResponseUtil.ok(map));
    }

    @GetMapping("/list")
    @RequiresPermissions("admin:user:list")
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"},button = "查询")
    public Object list(HttpServletRequest req) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String username = req.getParameter("username");
        String mobile = req.getParameter("mobile");
        List<MarketUser> userList = adminUserService.list(page, limit, sort, order,username, mobile);
        return ResponseUtil.okList(userList);
    }
}
